package com.cjst.service.impl.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.catalina.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cjst.dao.mapper.sys.SysRoleMapper;
import com.cjst.pojo.sys.SysRole;
import com.cjst.pojo.sys.SysUser;
import com.cjst.service.impl.base.BaseServiceImpl;
import com.cjst.service.sys.SysRoleService;
import com.cjst.service.sys.SysUserService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.UserPrincipalUtil;

/**
 * 后台用户  服务层
 * @author yj
 *
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUser, String>  implements SysUserService{
	
	@Autowired
	private SysRoleService roleService;
	
	/**
	 * 根据公司和角色类型查询用户列表
	 */
	public List<SysUser> listByOfficeAndRoleType(String roleType,SysUser user){
		if(StringUtils.isNotEmpty(roleType)&&user!=null) {
			
			Map<String, Object> map =  UserPrincipalUtil.getOfficePrincipals(user);
			if("-1".equals(map.get("auth"))) {
				return null;
			}
			
			
			WherePrams rolePrams = Method.where("delFlag", C.EQ, "0");
			rolePrams.and("type", C.EQ, "4");
			
			List<SysRole> roles = roleService.list(rolePrams);
			List<String> roleIds = new ArrayList<>();
			if(roles!=null&&roles.size()>0) {
				for (SysRole role : roles) {
					roleIds.add(role.getId());
				}
			}else {
				roleIds.add("");
			}
			WherePrams userPrams = Method.where("status", C.EQ, "1");
			userPrams.andIn("roleId", C.IN, roleIds);
			if("2".equals(map.get("auth"))) {
				List<String> officeIds = (List<String>) map.get("list");
				userPrams.andIn("officeId", C.IN, officeIds);
			}
			List<SysUser> users = this.list(userPrams);
			if(users!=null&&users.size()>0) {
				return users;
			}
		}
		return null;
	}
	
}
